/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-11 下午1:45:34
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.ShopCompanyModel;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.dao.Ana2ndDataDao;
import com.ywt.cda.dao.AnaImportDataDao;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.domain.enums.ParamFlag;
import com.ywt.cda.domain.enums.ShopDimensionFlag;
import com.ywt.cda.service.ConfDataCacheService;
import com.ywt.cda.service.GovAnalysisService;
import com.ywt.cda.service.ShopCompanyService;

/**
 * （一句话功能简述）
 * <br>（功能详细描述）
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-11 下午1:45:34
 */
@Service
public class ShopCompanyServiceImpl implements ShopCompanyService {

	@Resource
	private GovAnalysisService govAnalysisService;
	@Resource
	private Ana2ndDataDao ana2ndDataDao;
	@Resource
	private AnaImportDataDao anaImportDataDao;
	@Resource
	private ConfDataCacheService cdc;
	private final String YYYY_MM="yyyy-MM";
	@Override
	public List<Ana2ndData> getTopNShopData(ShopCompanyModel model, int topN,
			OrderByEnum orderBy) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_DATA_2ND_GOV_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_2ND_GOV_M;
		}
		List<Ana2ndData> datas = govAnalysisService.queryTotalDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				DimensionFlag.SHOPS_COMPANY.getCode(), orderBy.name(),"t.MAIN_CODE", page);
		return datas;
	}
	
	@Override
	public <T extends Ana2ndData> void toJsonArray(List<T> data,JSONArray jsonArray,OrderByEnum orderByEnum) {
		// TODO Auto-generated method stub
		
		if (data != null) {
			BigDecimal sumNum=new BigDecimal(0);
			BigDecimal sumPrice=new BigDecimal(0);
			BigDecimal sumPkg=new BigDecimal(0);
			for (int i = 0; i < data.size(); i++) {
				T ana2ndData = data.get(i);
				sumNum=sumNum.add(ana2ndData.getSumNum());
				sumPrice=sumPrice.add(ana2ndData.getSumPrice_());
				sumPkg=sumPkg.add(ana2ndData.getSumPkg_());
				
			}
			for (int i = 0; i < data.size(); i++) {
				T ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();
				
				String categoryName = cdc.getName(ParamFlag.SHOP_COMPANY,
						ana2ndData.getMainCode());
				if(null==categoryName){
					categoryName=ana2ndData.getMainCode();
				}
				nameValue.put("name", categoryName);
				
				switch (orderByEnum) {
				case SUM_NUM:
					nameValue.put("value", ana2ndData.getSumNum());
					BigDecimal percent;
					if(ana2ndData.getSumNum().equals(BigDecimal.ZERO)){
						percent = new BigDecimal(0);
					}else{
						percent=ana2ndData.getSumNum().divide(sumNum,4,BigDecimal.ROUND_HALF_UP);
						percent=percent.multiply(new BigDecimal(100));
						percent=percent.setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					nameValue.put("percent", percent);
					break;
				case SUM_PRICE:
					nameValue.put("value", ana2ndData.getSumPrice_());
					BigDecimal percent1;
					if(ana2ndData.getSumPrice().equals(BigDecimal.ZERO)){
						percent1 = new BigDecimal(0);
					}else{
						percent1=ana2ndData.getSumPrice_().divide(sumPrice,4,BigDecimal.ROUND_HALF_UP);
						percent1=percent1.multiply(new BigDecimal(100));
						percent1=percent1.setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					nameValue.put("percent", percent1);
					break;
				case SUM_PKG:
					nameValue.put("value", ana2ndData.getSumPkg_());
					BigDecimal percent2;
					if(ana2ndData.getSumPkg().equals(BigDecimal.ZERO)){
						percent2 = new BigDecimal(0);
					}else{
						percent2=ana2ndData.getSumPkg_().divide(sumPkg,4,BigDecimal.ROUND_HALF_UP);
						percent2=percent2.multiply(new BigDecimal(100));
						percent2=percent2.setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					nameValue.put("percent", percent2);
					break;
				case AVG_PRICE:
					nameValue.put("value", ana2ndData.getAvgPrice());
					break;
				default:
					
					break;
				}
				

				jsonArray.add(nameValue);

			}

		}
	}
	@Override
	public <T extends Ana2ndData> void toJsonArraySku(List<T> data,JSONArray jsonArray) {
		// TODO Auto-generated method stub
		if (data != null) {
			BigDecimal totalSku=new BigDecimal(0);
			for (int i = 0; i < data.size(); i++) {
				T ana2ndData = data.get(i);
				totalSku=totalSku.add(ana2ndData.getMainCodeCount());
			}
			for (int i = 0; i < data.size(); i++) {
				T ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();
				
				String categoryName = cdc.getName(ParamFlag.SHOP_COMPANY,
						ana2ndData.getCode());
				if(null==categoryName){
					categoryName=ana2ndData.getCode();
				}
				nameValue.put("name", categoryName);
				nameValue.put("value", ana2ndData.getMainCodeCount());
				BigDecimal percent1;
				if(ana2ndData.getMainCodeCount().equals(BigDecimal.ZERO)){
					percent1 = new BigDecimal(0);
				}else{
					percent1=ana2ndData.getMainCodeCount().divide(totalSku,4,BigDecimal.ROUND_HALF_UP);
					percent1=percent1.multiply(new BigDecimal(100));
					percent1=percent1.setScale(2, BigDecimal.ROUND_HALF_UP);
				}
				nameValue.put("percent", percent1);
				jsonArray.add(nameValue);

			}

		}
	}
	@Override
	public List<Ana2ndData> getTopNShopSku(ShopCompanyModel model, int topN) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_DATA_2ND_SHOP_D;
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", model.getStartTime_F());
		params.put("endDate", model.getEndTime_F());
		
		params.put("tableName", table.getTableName());
		
		params.put("mainFlag", ShopDimensionFlag.SKU.getCode());
		
		
		List<Ana2ndData> ana2ndDatas=ana2ndDataDao.listTotalSku(params, page);
		return ana2ndDatas;
	}

	@Override
	public <T extends Ana2ndData> void toJsonObject(List<T> data, JSONObject jsonObject,
			OrderByEnum orderByEnum) {
		if (data != null) {
			JSONArray nameArray = new JSONArray();
			JSONArray valueArray = new JSONArray();
			JSONArray nameValue=new JSONArray();
			for (int i = 0; i < data.size(); i++) {
				T ana2ndData = data.get(i);
				JSONObject nameValue0 = new JSONObject();
				String categoryName = cdc.getName(ParamFlag.SHOP_COMPANY,
						ana2ndData.getMainCode());
				if(null==categoryName){
					categoryName=ana2ndData.getMainCode();
				}
				nameValue0.put("name", categoryName);
				nameValue0.put("value", ana2ndData.getAvgPrice());
				nameValue.add(nameValue0);
				nameArray.add( categoryName);
				
				
				valueArray.add( ana2ndData.getAvgPrice());
				
				
			}
			
			jsonObject.put("nameArray", nameArray);
			jsonObject.put("valueArray", valueArray);
			jsonObject.put("nameValue", nameValue);
		}
	}

	@Override
	public List<AnaImportData> getTopNShopImportData(ShopCompanyModel model,
			int topN, OrderByEnum orderBy) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_IMPORT_DATA_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		
		List<AnaImportData> datas = govAnalysisService.queryTotalImportDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				DimensionFlag.SHOPS_COMPANY.getCode(), orderBy.name(),"t.DIMENSION_CODE", page);
		return datas;
	}

	@Override
	public List<Ana2ndData> getTopNShopImportSku(ShopCompanyModel model,
			int topN) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_2ND_SHOP_D;
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", model.getStartTime_F());
		params.put("endDate", model.getEndTime_F());
		
		params.put("tableName", table.getTableName());
		
		params.put("mainFlag", ShopDimensionFlag.SKU.getCode());
		
		
		List<Ana2ndData> ana2ndDatas=ana2ndDataDao.listTotalSku(params, page);
		return ana2ndDatas;
	}

	@Override
	public void toJsonArrayImport(List<AnaImportData> data,
			JSONArray jsonArray, OrderByEnum orderByEnum) {
		// TODO Auto-generated method stub
		if (data != null) {
			BigDecimal sumNum=new BigDecimal(0);
			BigDecimal sumPrice=new BigDecimal(0);
			BigDecimal sumPkg=new BigDecimal(0);
			for (int i = 0; i < data.size(); i++) {
				AnaImportData ana2ndData = data.get(i);
				sumNum=sumNum.add(ana2ndData.getSumNum());
				sumPrice=sumPrice.add(ana2ndData.getSumPrice_());
				sumPkg=sumPkg.add(ana2ndData.getSumPkg_());
				
			}
			for (int i = 0; i < data.size(); i++) {
				AnaImportData ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();
				
				String categoryName = cdc.getName(ParamFlag.SHOP_COMPANY,
						ana2ndData.getDimensionCode());
				if(null==categoryName){
					categoryName=ana2ndData.getDimensionCode();
				}
				nameValue.put("name", categoryName);
				
				switch (orderByEnum) {
				case SUM_NUM:
					nameValue.put("value", ana2ndData.getSumNum());
					
					BigDecimal percent=ana2ndData.getSumNum().divide(sumNum,4,BigDecimal.ROUND_HALF_UP);
					percent=percent.multiply(new BigDecimal(100));
					percent=percent.setScale(2, BigDecimal.ROUND_HALF_UP);
					nameValue.put("percent", percent);
					break;
				case SUM_PRICE:
					nameValue.put("value", ana2ndData.getSumPrice_());
					
					BigDecimal percent1=ana2ndData.getSumPrice_().divide(sumPrice,4,BigDecimal.ROUND_HALF_UP);
					percent1=percent1.multiply(new BigDecimal(100));
					percent1=percent1.setScale(2, BigDecimal.ROUND_HALF_UP);
					nameValue.put("percent", percent1);
					break;
				case SUM_PKG:
					nameValue.put("value", ana2ndData.getSumPkg_());
					BigDecimal percent2=ana2ndData.getSumPkg_().divide(sumPkg,4,BigDecimal.ROUND_HALF_UP);
					percent2=percent2.multiply(new BigDecimal(100));
					percent2=percent2.setScale(2, BigDecimal.ROUND_HALF_UP);
					nameValue.put("percent", percent2);
					break;
				case AVG_PRICE:
					nameValue.put("value", ana2ndData.getAvgPrice());
					break;
				default:
					
					break;
				}
				

				jsonArray.add(nameValue);

			}

		}
	}

	@Override
	public void toJsonObjectImport(List<AnaImportData> data,
			JSONObject jsonObject, OrderByEnum orderByEnum) {
		// TODO Auto-generated method stub
		if (data != null) {
			JSONArray nameArray = new JSONArray();
			JSONArray valueArray = new JSONArray();
			JSONArray nameValue=new JSONArray();
			for (int i = 0; i < data.size(); i++) {
				AnaImportData ana2ndData = data.get(i);
				JSONObject nameValue0 = new JSONObject();
				String categoryName = cdc.getName(ParamFlag.SHOP_COMPANY,
						ana2ndData.getDimensionCode());
				if(null==categoryName){
					categoryName=ana2ndData.getDimensionCode();
				}
				nameValue0.put("name", categoryName);
				nameValue0.put("value", ana2ndData.getAvgPrice());
				nameValue.add(nameValue0);
				nameArray.add( categoryName);
				
				
				valueArray.add( ana2ndData.getAvgPrice());
				
				
			}
			
			jsonObject.put("nameArray", nameArray);
			jsonObject.put("valueArray", valueArray);
			jsonObject.put("nameValue", nameValue);
		}
	}

}
